(function() {
  app.controller('marketViewsCtrl', function($state, $scope, quoteApi, local_m, $rootScope, header_m, $interval) {
    var tab_type = $state.current.name;
    local_m.set('market_tab_index', tab_type);

    header_m.set({
      css_l: '',
      fun_l: function() { return; },
      tab_show: true,
      tabs: [
        { txt: '自选', go: '/zixuan' },
        { txt: '市场', go: '/' + tab_type }
      ],
      tab_go: function(it) { $state.go(it.go.substring(1)); },
      css_r: { name: 'icon-search' },
      fun_r: function() { $state.go('search'); },
    });

    if (tab_type === '\market.hs') {
      var arr = ['399005', '1B0300', '1B0007', '1B0016', '399106'];
      var obj = [
        { board_id: '1110', sort_index: 34, type_name: '涨幅榜', top_type: 'paihang', top_sort: 0, show_flag: true },
        { board_id: '1110', sort_index: 34, type_name: '跌幅榜', top_type: 'paihang', top_sort: 1, sort_mode: 1, show_flag: true },
        { board_id: '1110', sort_index: 37, type_name: '换手率榜', top_type: 'paihang', top_sort: 2, show_flag: true },
        { board_id: '1110', sort_index: 33, type_name: '量比榜', top_type: 'paihang', top_sort: 3, show_flag: true }
      ];
      $scope.zhishu = [];
      $scope.paihang = [];

      $scope.init = function(type) {
        quoteApi
          .query(new quoteApi.QuoteApi.HSindex0x2b({
            sublist: [
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 34
            ],
          }))
          .then(function(data) {
            $scope.zhishu = [];
            for (var i = 0; i < data.length; i++) {
              for (var j = 0; j < arr.length; j++) {
                if (data[i].stkinfo.code === arr[j]) {
                  $scope.zhishu.push(data[i]);
                }
              }
            }
          });

        angular.forEach(obj, function(item, index) {
          item.sublist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 33, 34, 37];
          quoteApi
            .query(new quoteApi.QuoteApi.BoardRank0x28(item))
            .then(function(data) {
              if (type === 'reload') {
                if ('data' in $scope.paihang[index]) {
                  $scope.paihang[index].data = data.data;
                }
              } else {
                data.show_flag = item.show_flag;
                data.type_name = item.type_name;
                data.top_type = item.top_type;
                data.top_sort = item.top_sort;
                item.sort_mode ? data.top_sort_mode = item.sort_mode : '';
                $scope.paihang[index] = data;
              }
            });
        });
      }
    } else if (tab_type === '\market.bk') {
      var obj = [
        { category: '1016', type_name: '领涨概念', top_type: 'bankuai', top_sort: 0, show_flag: true },
        { category: '1011', type_name: '领涨行业', top_type: 'bankuai', top_sort: 1, show_flag: true },
        { category: '1010', type_name: '领涨地区', top_type: 'bankuai', top_sort: 2, show_flag: true }
      ];
      $scope.bankuai = [];

      $scope.init = function(type) {
        angular.forEach(obj, function(item, index) {
          item.sublist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 34, 1015];
          item.length = 8;
          item.sortfd_index = 34;
          quoteApi
            .query(new quoteApi.QuoteApi.BoradIn0X2A(item))
            .then(function(data) {
              if (type === 'reload') {
                if ('data' in $scope.bankuai[index]) {
                  $scope.bankuai[index].data = data.data;
                }
              } else {
                data.show_flag = item.show_flag;
                data.type_name = item.type_name;
                data.top_type = item.top_type;
                data.top_sort = item.top_sort;
                $scope.bankuai[index] = data;
              }
            });
        });
      }
    } else if (tab_type === '\market.cy') {
      var obj = [
        { board_id: '1106', sort_index: 34, type_name: '涨幅榜', top_type: 'paihang_cy', top_sort: 0, show_flag: true },
        { board_id: '1106', sort_index: 37, type_name: '换手率榜', top_type: 'paihang_cy', top_sort: 1, show_flag: true },
        { board_id: '1106', sort_index: 33, type_name: '量比榜', top_type: 'paihang_cy', top_sort: 2, show_flag: true }
      ];
      $scope.paihang_cy = [];

      $scope.init = function(type) {
        angular.forEach(obj, function(item, index) {
          item.sublist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 33, 34, 37];
          quoteApi
            .query(new quoteApi.QuoteApi.BoardRank0x28(item))
            .then(function(data) {

              if (type === 'reload') {
                if ('data' in $scope.paihang_cy[index]) {
                  $scope.paihang_cy[index].data = data.data;
                }
              } else {
                data.show_flag = item.show_flag;
                data.type_name = item.type_name;
                data.top_type = item.top_type;
                data.top_sort = item.top_sort;
                $scope.paihang_cy[index] = data;
              }
            });
        });
      }
    }

    var timer = $interval(function() {
      $scope.init('reload');
    }, 3000);
    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });
  });
})();